<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\CreateFriendRequest;
use App\Friend;
use MyFlash;
class FriendController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $link=Friend::orderBy('sort','desc')->paginate(10);
       return view('admin.friend.index',compact('link'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.friend.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreateFriendRequest $request)
    {
        $friend=new Friend;
        $friend->title=$request->title;
        $friend->url=$request->url;
        $friend->sort=$request->sort;
        $friend->key=$request->key;
        if($friend->save()){
            MyFlash::success('添加成功');
            return redirect()->route('admin.friend.index');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $link=Friend::findOrFail($id);
        return view('admin.friend.edit',compact('link'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(CreateFriendRequest $request, $id)
    {
        $link=Friend::findOrFail($id);
        $link->title=$request->title;
        $link->url=$request->url;
        $link->sort=$request->sort;
        $link->key=$request->key;
        if($link->save()){
            MyFlash::success('编辑成功');
            return redirect()->route('admin.friend.index');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id){
        if(Friend::destroy($id)){
            MyFlash::success('删除成功');
            return redirect()->route('admin.friend.index');
        }
    }
}
